﻿using System;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using Framework.Interface;

namespace Framework.DataObjects
{
    /// <summary>
    /// Document Object Models are simple objects that store data and can render themselves out using XML
    /// </summary>
    /// <remarks>
    /// Deprecated, use the AsXml extension method instead
    /// </remarks>
    [Serializable]
    public abstract class DomObject : IToXml
    {
        /// <summary>
        /// Render out as XML
        /// </summary>
        /// <returns></returns>
        public string ToXml()
        {
            var output = new MemoryStream();
            var serializer = new XmlSerializer(GetType());

            serializer.Serialize(output, this);

            var result = Encoding.UTF8.GetString(output.ToArray());

            output.Close();

            return result;
        }

        /// <summary>
        /// Render out as XML, provide any types that are required by the XML Serializer for type resolution
        /// </summary>
        /// <param name="extraTypes"></param>
        /// <returns></returns>
        public string ToXml(Type[] extraTypes)
        {
            var output = new MemoryStream();
            var serializer = new XmlSerializer(GetType(), extraTypes);

            serializer.Serialize(output, this);

            var result = Encoding.UTF8.GetString(output.ToArray());

            output.Close();

            return result;
        }
    }
}
